package com.tajiang.business.bluetooth;
import java.util.List;
import java.util.Map;

import com.tajiang.business.R;
import com.tajiang.business.common.application.TJApp;
import com.tajiang.business.common.utils.LogUtils;
import com.tajiang.business.setting.dialog.BTConnectDialog;
import com.tajiang.business.setting.fragment.SettingFragment;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

public class ListViewAdapter extends BaseAdapter{
    public final static String DEBUG_TAG="MyAdapter";
    public static final String IMG = "img";
    public static final String DEVICE_NAME = "device_name";
    public static final String STATUS = "status";
    public static final String INFO = "info";
    public static final String BT_ENABLE = "btenable";
    public static final String ENABLE = "enable";
    public static final String DISABLE="disable";
    public static final int MESSAGE_CONNECT = 1;
    private Handler mHandler= null;
    private List<Map<String, Object>> listItems;    //商品信息集合
    private LayoutInflater listContainer;           //视图容器

    public final class ListItemView{                //自定义控件集�?
        private TextView deviceName;
        private TextView info;
        private Button button;
    }

    ListItemView  listItemView = null;

    private onButtonClickListener buttonClicklistener;

    public void setButtonClicklistener(onButtonClickListener buttonClicklistener) {
        this.buttonClicklistener = buttonClicklistener;
    }

    public interface onButtonClickListener{
        public void onButtonClick();
    }

    public ListViewAdapter(Context context,List<Map<String, Object>> listItems,
                           Handler handler) {
        listContainer = LayoutInflater.from(context);   //创建视图容器并设置上下文
        this.listItems = listItems;
        mHandler = handler;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listItems.size();
    }
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stubho
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        //      ListItemView  listItemView = null;
        if (arg1 == null) {
            listItemView = new ListItemView();
            arg1 = listContainer.inflate(R.layout.blue_tooth_main_screen_list_item, null);
//            listItemView.image = (ImageView)arg1.findViewById(R.id.ivOperationItem);
//            listItemView.title = (TextView)arg1.findViewById(R.id.tvOperationItem);
            listItemView.info = (TextView)arg1.findViewById(R.id.tvInfo);
            listItemView.button= (Button)arg1.findViewById(R.id.btTestConnect);
            listItemView.deviceName = (TextView) arg1.findViewById(R.id.tv_device_name);

            //设置控件集到convertView
            arg1.setTag(listItemView);
        }else {
            listItemView = (ListItemView)arg1.getTag();
        }
        final int arg = arg0;
//        listItemView.image.setBackgroundResource((Integer) listItems.get(
//                arg0).get(IMG));
//        listItemView.title.setText((String) listItems.get(arg0)
//                .get(TITEL));
        listItemView.info.setText((String) listItems.get(arg0).get(INFO));
        listItemView.button.setText((String) listItems.get(arg0).get(STATUS));
        listItemView.deviceName.setText("设备名称：" + (String)listItems.get(arg0).get(DEVICE_NAME));
//        final String isHaveBluetooth = (String)listItems.get(arg0).get(INFO);
//        if (isHaveBluetooth.equals(TJApp.getInstance().getString(R.string.init_port_info))) {
//            listItemView.button.setText("选择设备");
//            listItemView.button.setClickable(false);
//            listItemView.button.setBackgroundDrawable(TJApp.getInstance().getDrawable(R.drawable.shape_rect_round_gray2));
//        } else {
//            listItemView.button.setText("连接");
//            listItemView.button.setClickable(true);
//            listItemView.button.setBackgroundDrawable(TJApp.getInstance().getDrawable(R.drawable.shape_line_round_blue));
//        }

        String str = (String)listItems.get(arg0).get(BT_ENABLE);
        if(str.equals(ENABLE)) {
            listItemView.button.setEnabled(true);
        }
        else {
            listItemView.button.setEnabled(false);
        }
        listItemView.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d(DEBUG_TAG,"arg1 " + arg);
                Message message = new Message();
                message.what = MESSAGE_CONNECT;
                message.arg1 = arg;
                listItemView.button.getTag();
                mHandler.sendMessage(message);
            }
        });
        return arg1;
    }


}

